package com.hfzy.ihk.web.linkcall.support.aop;

import com.hfzy.ihk.common.util.Utils;
import com.hfzy.ihk.facade.linkcall.enums.Bool;
import com.hfzy.ihk.facade.userCenter.entity.UserInfo;
import com.hfzy.ihk.web.linkcall.context.Context;
import com.hfzy.ihk.web.linkcall.context.ContextKey;
import com.hfzy.ihk.web.linkcall.utils.UserUtils;
import com.hfzy.ihk.web.linkcall.utils.WebResultUtils;
import com.hfzy.ihk.web.oauth.client.utils.UserUtil;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.RequestContextHolder;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.Map;

/**
 * 此类是 有 @PreProcessParam 注解 方法的切面
 * 主要进行数据预处理的操作类
 * */
@Aspect
@Component
public class PreProcessParamHandle {
    private static final Logger logger = LoggerFactory.getLogger(PreProcessParamHandle.class);

    /**
     * +
     "|| execution(* com.hfzy.ihk.web.linkcall.controller.service.RecordFileController.*(..)) " +
     "|| execution(* com.hfzy.ihk.web.linkcall.controller.service.WeChatAppController.*(..)) "
     * */

    @Pointcut("@annotation(com.hfzy.ihk.web.linkcall.support.annotation.PreProcessParam) " )
    public void pointCutFun(){};

    @Around(value = "pointCutFun()")
    Object  pointCutFunAround(ProceedingJoinPoint proceedingJoinPoint) throws Throwable {
        logger.debug("进入执行预处理切面操作方法");
        Object[] args = proceedingJoinPoint.getArgs();
        try {
            String funName = proceedingJoinPoint.getSignature().getName();
            HttpServletRequest httpServletRequest = Context.contextIntf.getRequest();
            if(args!=null && args.length>0){
                if(args[0] instanceof Map){
                    logger.debug("执行 预处理数据 操作");
                    Map params = (Map)args[0];
                    UserInfo user = UserUtil.getLoginUser();
                    if(user==null) { // 如果不是当前登录用户，根据传参数 createUserId 获取 user
                        String createUserId = httpServletRequest.getParameter("createUserId");
                        if(!Utils.isNullString(createUserId))
                            user = UserUtils.getSimpleUserById(createUserId);
                    }
                    if(user!=null){ //如果user 不为空
                        boolean flag = UserUtils.hasSearchAllPriv(user); //判断查所有数据的权限
                        if(!flag){ // 无法查询所有
                            params.put("createUserId",user.getId().toString());
                            args[0] = params;
                        }else{
                            if(params.containsKey("createUserId")){
                                params.remove("createUserId");
                            }
                        }
                    }else{
                        //放开访问的链接的，没有用户id，暂时放开数据权限的校验
//                       throw new IllegalAccessException("参数异常,操作失败");
                    }
                }
            }
        }catch (Exception e){
            logger.error("执行参数预处理操作异常"+e.getMessage());
        }
        Object obj  = proceedingJoinPoint.proceed(args);
        logger.debug("退出执行预处理切面操作方法");
        return obj;
    }
}
